window.Android = function() {
    let bridgeFrame = document.createElement('iframe');
    bridgeFrame.style = "display: none";
    document.body.append(bridgeFrame);
    let curActionId = 0;
    let actions = {};
    let invoke = (cmd, params, callback) => {
        let callId = ++curActionId;
        actions[callId] = {"callback": callback};
        let paramsStr = JSON.stringify(params);
        console.debug(`JavaScript请求调用安卓端: cmd=${cmd}, params=${paramsStr}`);
        bridgeFrame.src = `jsbridge://${cmd}/${callId}/${encodeURI(paramsStr)}`;
    };
    let callback = (data) => {
        /** data 格式为 {callId: Number, params: JSON} */
        let callId = data.callId;
        let params = data.params;
        console.debug(`安卓端回调JavaScript: callId=${callId}, params=${JSON.stringify(params)}`);
        let action = actions[callId];
        if (!action) {
            console.warn(`callId= ${callId} 不存在callback函数`);
            return;
        }
        action.callback(params);
    };
    return {invoke: invoke, callback: callback};
}();

/* JavaScript调用安卓例子 */
/* window.Android.invoke('log', {name: 'hello'}, (data) => {
    console.log('接收到callback:' + JSON.stringify(data));
});*/
